package net.huashitong.Indicators.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.Analysis;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.AnalysisService;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.SocialZeroService;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.Indicators.utils.CodeUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.ThiTitleUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
/**
 * 社零
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/socialZero")
public class SocialZeroController extends JsonBaseController {
	@Autowired
	private SocialZeroService socialZeroService;
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private CompletionClassificationService classificationService;
	@Autowired
	private AnalysisService analysisService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 社零二级界面接口
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping("/querySLSecView")
	public Message querySLSecView(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		//规上工业全市各县区排名情况
		IndicatorBreakdownDatabean databean = socialZeroService.queryCountyRank(filter);
		if(ObjectUtils.isNotBlank(databean)){
			map.put("type", "1");
			map.put("dataInner", databean);
			map.put("unit", "亿元");
			map.put("title", "该指标全市排名一览");
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
			map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GXQZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GXQJDZ);
			map.put("secTitle", "社会消费品零售总额全市各县区排名情况");
			maps.add(map);
		}
		
		// 规上工业当季同比增长情况
//		filter.setDate("201701-08");
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		List<IndicatorBreakdownDatabean> databeans = socialZeroService.queryDevelopmentSituation(filter);
		if(ObjectUtils.isNotBlank(databeans)){
			map = new HashMap<String, Object>();
			map.put("type", "2");
			map.put("data", databeans);
			map.put("title", "该指标发展情况");
			map.put("descOne", ThiTitleUtils.DYTBZZL);
			map.put("descTwo", ThiTitleUtils.DYLJZZL);
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
			map.put("secTitle", "社会消费品零售总额当月同比增长情况");
			maps.add(map);
		}
		
		
		//规上工业各季度绝对值及同比增长情况
//		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		filter.setDate(DateForIndUtils.getNextYearDate(date.substring(0,4),0));
		List<Map<String, Object>> list = socialZeroService.queryAbsoluteValue(filter);
		/*if(ObjectUtils.isNotBlank(list)){
			map = new HashMap<String, Object>();
			map.put("type", "3");
			map.put("data", list);
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
			map.put("unit", "亿元");
			map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GYTBZZL);
			map.put("descTwo", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYJDZ);
			map.put("descThi", date.substring(0, 4)+ThiTitleUtils.GYJDZ);
			map.put("title", "该指标各月同比增长情况");
			map.put("secTitle", "社会消费品零售总额各月"+ThiTitleUtils.JDZ+"及同比增长情况");
			maps.add(map);
		}*/
		
		
		//规上工业累计同比增长情况
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
//		filter.setDate("201701-08");
		list = socialZeroService.queryCumulativeGrowth(filter);
		if(ObjectUtils.isNotBlank(list)){
			map = new HashMap<String, Object>();
			map.put("type", "4");
			map.put("data", list);
			map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYTBZZL);
			map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GYTBZZL);
			map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYLJJDZ);
			map.put("descFou", date.substring(0, 4)+ThiTitleUtils.GYLJJDZ);
			map.put("unit", "亿元");
			map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
			map.put("title", "该指标累计同比增长情况(8月)");
			map.put("secTitle", "社会消费品零售总额累计"+ThiTitleUtils.JDZ+"及同比增长情况");
			maps.add(map);
		}
		
		
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
//		filter.setDate("201701-08");
		List<CompletionClassificationDatabean> classificationDatabean2s = socialZeroService.queryByXZValue(filter);
	  if(ObjectUtils.isNotBlank(classificationDatabean2s)){
			map = new HashMap<String, Object>();
			map.put("type", "5");
			map.put("unit", "万元");
			map.put("data", classificationDatabean2s);
			map.put("category", CodeUtils.category_XZ);
			map.put("title", "该指标各乡镇同比增长情况(8月)");
			map.put("secTitle", "社会消费品零售总额各乡镇运行情况");
			maps.add(map);
		}
		
		
//		filter.setDate("201701-07");
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		classificationDatabean2s = socialZeroService.queryByDWValue(filter);
		if(ObjectUtils.isNotBlank(classificationDatabean2s)){
			map = new HashMap<String, Object>();
			map.put("type", "10");
			map.put("data", classificationDatabean2s);
			map.put("unit", "万元");
			map.put("title", "该指标需关注单位运行情况(7月)");
			map.put("category", CodeUtils.category_QY);
			map.put("secTitle", "社会消费品零售总额需关注企业运行情况情况");
			maps.add(map);
		}
		
		
		List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>();
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		filter.setQy("商贸业");
		List<CompletionClassification> classificationds = classificationService.queryByDWValue(filter);
		if(ObjectUtils.isNotBlank(classificationds)){
			/*int size = classificationds.size();
			size = size/20;*/
			for (int i = 0; i <1; i++) {
				//商贸业企业完成情况
				filter.setDate(date);
				filter.setCurrentMonth(cuDate);
				filter.setPage(i);
				classificationDatabeans = socialZeroService.queryBySMYValue(filter);
				if(ObjectUtils.isNotBlank(classificationDatabeans)){
					map = new HashMap<String, Object>();
					map.put("type", "10");
					map.put("data", classificationDatabeans);
					map.put("descThi", date.substring(0, 4)+ThiTitleUtils.LJWCZ);
					map.put("descTwo", DateForIndUtils.getNextYearDate(date.substring(0, 4), -1)+ThiTitleUtils.TQWCZ);
					map.put("category", CodeUtils.category_SHY);
					map.put("title", "该指标各商贸业完成情况");
					map.put("unit", "万元");
					map.put("secTitle", "第三产业各商贸业企业完成情况");
					maps.add(map);
				}
			}
		}
		
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		filter.setQy("服务业");
		classificationds = classificationService.queryByDWValue(filter);
		if(ObjectUtils.isNotBlank(classificationds)){
			/*int sizese = classificationds.size();
			sizese = sizese/20;*/
			for (int i = 0; i < 1; i++) {
				//服务业企业完成情况
				filter.setDate(date);
				filter.setCurrentMonth(cuDate);
				filter.setPage(i);
				classificationDatabeans = socialZeroService.queryBySMYValue(filter);
				if(ObjectUtils.isNotBlank(classificationDatabeans)){
					map = new HashMap<String, Object>();
					map.put("type", "10");
					map.put("data", classificationDatabeans);
					map.put("descThi", date.substring(0, 4)+ThiTitleUtils.LJWCZ);
					map.put("descTwo", DateForIndUtils.getNextYearDate(date.substring(0, 4), -1)+ThiTitleUtils.TQWCZ);
					map.put("category", CodeUtils.category_FHY);
					map.put("title", "该指标各服务业完成情况");
					map.put("unit", "万元");
					map.put("secTitle", "第三产业各服务业企业完成情况");
					maps.add(map);
				}
			}
		}
		
		map = new HashMap<String, Object>();
		filter.setCurrentMonth(cuDate);
		Analysis analysis = analysisService.getByIndIdAndDate(filter);
		if(ObjectUtils.isNotBlank(analysis)){
		    	map.put("secTitle", analysis.getText());
		 		map.put("type", "0");
		 		map.put("title", "该指标分析");
		 		maps.add(map);
	    }
		
		map = new HashMap<String, Object>();
		filter.setCurrentMonth(cuDate);
	    TertiaryIndustry industry = industryService.queryByfilter(filter);
	    if(ObjectUtils.isNotBlank(industry)){
	    	map.put("leadership", industry.getLeadership());
	 		map.put("unitsNumber", industry.getUnitsNumber());
	    }
		map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
		map.put("lists", maps);
		map.put("title", "社会消费品零售总额运行情况");
		return coverMessage("200", "获取成功",map);
	}
}
